<?php
// $Id$

/**
 * Administration page callback
 */


function jadwal_admin_setting(&$form_state) {
  $default = !empty($form_state['values']['numberof_freeday']) ? $form_state['values']['numberof_freeday'] : 0;
  $days = array(t('Sunday'), t('Monday'), t('Tuesday'), t('Wednesday'), t('Thursday'), t('Friday'), t('Saturday'));
  $form['jadwal_workhour'] = array(
    '#type' => 'textfield',
    '#title' => 'Work hour',
    '#description' => t('School work hour (in hour)'),
    '#size' => 3,
    '#default_value' => variable_get('jadwal_workhour', 8)
  );
  
  $form['jadwal_freeday'] = array(
    '#type' => 'select',
    '#title' => 'Free day',
    '#description' => t('Free day for a week'),
    '#options' => $days,
    '#default_value' => variable_get('jadwal_freeday', 'sunday')
  );
  
  $form['jadwal_periodstart'] = array(
    '#type' => 'date',
    '#title' => 'Starting period',
    '#description' => t('Starting period date. This date is the date where schools time start. end of this period is a year.'),
    '#default_value' => variable_get('jadwal_periodstart', NULL)
  );

  // stole from system_settings_form($form)
  $form['buttons']['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save configuration'),
  );
  $form['buttons']['reset'] = array(
    '#type' => 'submit',
    '#value' => t('Reset to defaults'),
  );

  if (!empty($_POST) && form_get_errors()) {
    drupal_set_message(t('The settings have not been saved because of the errors.'), 'error');
  }
  $form['#submit'][] = 'jadwal_admin_getinterval';
  $form['#theme'] = 'system_settings_form';
  return $form;
}

/**
 * Implementation of hook_validate()
 */
function jadwal_admin_setting_validate($form, $form_state) {
if (!is_numeric($form_state['values']['jadwal_workhour'])) {
     form_set_error('druais', t('Please enter a number'));
   }
}

/**
 * Get interval between period
 */
function jadwal_admin_getinterval($form, $form_state) {
  system_settings_form_submit($form, $form_state);
  $intervalstart = variable_get('jadwal_periodstart', $form_state['values']['jadwal_periodstart']);
  $instart = mktime(0, 0, 0, $intervalstart['month'], $intervalstart['day'], $intervalstart['year']);
  $inend = $instart+YEAR;
  
  $a = 0; //counter
  $time = $instart;
  while ($time != $inend){
    $time = mktime(0, 0, 0, $intervalstart['month'], $intervalstart['day']+$a, $intervalstart['year']);
    if ($time == $instart+SEMESTER) {
      $emptyday[] = array($time, 'full'); 
    } elseif (date("w", $time) == variable_get('jadwal_freeday', 0)) {
      $freeday[] = $time;
    } else {
      $emptyday[] = $time;
    }
    $a++;
  } // endwhile
  
  variable_set('jadwal_emptyday', $emptyday);
  variable_set('jadwal_freedaylist', $freeday);
  
  $vid = db_result(db_query("SELECT vid FROM {vocabulary} WHERE module='jadwal'"));
  $numberof_class = variable_get('druais_numberof_class', 0);
  $vocabulary = array(
    'name' => t('Jadwal'),
    'help' => t('schedule related to this event'),
    'multiple' => '0',
    'required' => '1',
    'hierarchy' => '0',
    'relations' => '0',
    'tags' => '0',
    'nodes' => array('event' => 1),
    'module' => 'jadwal'
  );
  if (isset($vid)) {
    $vocabulary['vid'] = $vid; 
  }
  $saved = taxonomy_save_vocabulary($vocabulary);
  if ($saved == SAVED_NEW || $saved == SAVED_UPDATED) {
    $vid = $vocabulary['vid'];
    while ($numberof_class) {
      $term = array(
        'name' => t('Class ').$numberof_class,
        'vid' => $vid,
        'weight' => $numberof_class,
      );
      taxonomy_save_term($term);
      $numberof_class--;
    } // endwhile    
  } // endif
} // endfunction

// @TODO: buat fungsi untuk insert node baru, untuk jadwal insert event.